package rsj.service.remind;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import com.rlyy.aibase.utils.helpers.JsonHelper;
import common.entity.PageInfo;
import common.util.Assert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rsj.api.remind.entity.*;
import rsj.db.dbutils.UniqueID;
import rsj.sql.remind.RemindDao;
import rsj.sql.remind.RemindLogDao;
import rsj.sql.user.UserDao;
import rsj.util.PaginationData;
import rsj.utils.RedisUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component
public class RemindServiceImpl implements IRemindService {


    @Autowired
    private RemindLogDao remindLogDao;

    @Autowired
    private RemindDao remindDao;

    @Autowired
    private UserDao userDao;


    /**
     * 预警日志列表
     *
     * @param remindLogPage  页面查询
     * @return {@link PaginationData<TableNode>}
     */
    @Override
    public PaginationData<List<RemindLogTable>> remindLogList(RemindLogPage remindLogPage) {
        PaginationData<List<RemindLogTable>> paginationData = new PaginationData<>();
        Node node = remindLogDao.logCount();
        PageInfo pageQuery = remindLogPage.getPageInfo();
        TableNode tableNode = remindLogDao.listByPage(pageQuery.getPageSize(), pageQuery.getPageOffSet());
        paginationData.setTotalCount(node.valueInt());
        paginationData.setDataList(JSON.parseArray(tableNode.toJson(), RemindLogTable.class));
        return paginationData;
    }

    /**
     * 添加预警配置
     * @param remindInfo
     * @return
     */
    @Override
    public String addRemindInfo(RemindInfo remindInfo) {
        RemindTable remindTable = new RemindTable();
        BeanUtils.copyProperties(remindInfo, remindTable);
        MapNode remindInfoByType = remindDao.getRemindInfoByType(remindTable.getRemindType());
        Assert.isTrue(remindInfoByType.size()<=0,"已存在该预警类型，无法重复添加");
        String id = String.valueOf(UniqueID.generateId());
        remindTable.setRemindId(id);
        remindTable.setUpdateTm(LocalDateTime.now());
        remindDao.addRemindInfo(remindTable);
        return id;
    }

    /**
     * 更新预警配置
     * @param remindInfo
     */
    @Override
    public void updateRemindInfo(RemindInfo remindInfo) {
        RemindTable remindTable = new RemindTable();
        BeanUtils.copyProperties(remindInfo, remindTable);
        remindTable.setUpdateTm(LocalDateTime.now());
        remindDao.updateRemindInfo(remindTable);
        //TODO 是否要清除现有预警次数
        Set<String> keys = RedisUtils.keys("rsj|remind|*_"+remindInfo.getRemindType());
        RedisUtils.del(keys.toArray(new String[]{}));
    }

    /**
     * 根据条件查询预警配置
     * @param remindGet
     * @return
     */
    @Override
    public RemindInfo getRemindInfo(RemindGet remindGet) {
        MapNode remindInfoByType = remindDao.getRemindInfoByType(remindGet.getRemindType());
        RemindInfo remindInfo = JSONObject.parseObject(JsonHelper.mapNode2Json(remindInfoByType), RemindInfo.class);
        String userIds = remindInfo.getReceiveUserId();
        if(StringUtils.isEmpty(userIds)){
            remindInfo.setReceiveUser(JSONObject.toJSONString(new ArrayList<>()));
            return remindInfo;
        }
//        Assert.isNotBlank(userIds,"未查到对应预警配置");
        String[] userArr = userIds.split(",");
        List<ReceiveUser> receiveUserList = new ArrayList<>();
        for (String userId: userArr) {
            MapNode userMap = userDao.queryBaseInfoByUser(userId);
            receiveUserList.add(new ReceiveUser(userId, userMap.getValue("User_Name")));
        }
        remindInfo.setReceiveUserId(null); //清除防止序列化
        remindInfo.setReceiveUser(JSONObject.toJSONString(receiveUserList));
        return remindInfo;
    }
}
